<?php

class User_credit_model extends MY_Model{

    var $table = 'user_credit';

    public function __construct(){

        parent::__construct();
    }


    public function add($data = array()) {

        return $this->db->insert($this->table, $data);
    }

    public function get($options = array()){

        $this->db->select('*');
        $this->db->from($this->table);

        // WHERE
        parent::_where($options);

        $query = $this->db->get();

        if (!$query->num_rows())
            return false;

        return $query->row();
    }

    public function gets($options = array()){

        $this->db->select('SQL_CALC_FOUND_ROWS *', false);
        $this->db->from($this->table);


        $q = $this->db->get();

        if (!$q->num_rows()) {
            $result = array('rows' => false, 'total_rows' => 0);
            return $result;
        }

        $result = array(
            'rows' => $q->result(),
            'total_rows' => parent::_total_rows()
        );

        return $result;
    }

    public function edit($data = array(), $options = array()) {

        return $this->db->update($this->table, $data, $options);
    }


    /**
     * Kullanıcı kredisini verilen miktar kadar azaltır.
     *
     * @param int $user_id
     * @param int $quantity
     * @return bool
     */
    public function decrease($user_id = 0, $qty = 1) {

        $this->db->set('used', "used+{$qty}", false);
        $this->db->where('user_id', $user_id);
        return $this->db->update('user_credit');
    }

    /**
     * Kullanıcı kredisini verilen miktar kadar arttırır.
     *
     * @param int $user_id
     * @param int $quantity
     * @return bool
     */
    public function increase($user_id = 0, $qty = 1) {

        $this->db->set('total', "total+{$qty}", false);
        $this->db->where('user_id', $user_id);
        return $this->db->update('user_credit');
    }

}

/* End of file user_credit_model.php */
